<?php
/**
 * Controls access to subscriber only content.
 */
class ShoppSE_Content
{
	/**
	 * Contains a reference to the Shopp Magazine object.
	 * 
	 * @var Shopp_Magazine
	 */
	protected $subengine;

	/**
	 * List of post types which can be designated as member
	 * only content.
	 *
	 * @var array
	 */
	public $post_types = array('page', 'post');
	
	
	
	/**
	 * Puts restrictions in effect to limit access to subscriber-only
	 * content.
	 * 
	 * @param ShoppSE $subengine
	 * @return ShoppSE_Content
	 */
	public function __construct(ShoppSE $subengine)
	{
		$this->subengine = $subengine;

		// Register hooks
		add_action('add_meta_boxes', array($this, 'post_metabox'));
		add_action('save_post', array($this, 'update_post_setting'));
		add_filter('the_content', array($this, 'access_policy'));
	}


	/**
	 * Registers metaboxes for supported post types (by default this 
	 * is just regular pages and posts).
	 */
	public function post_metabox()
	{
		// Add metabox(es) to post editor screens
		foreach ($this->post_types as $type)
			add_meta_box('shoppme_memberonlybox', __('Magazine Settings', 'Shopp'),
		        array($this, 'render_metabox'), $type, 'side', 'high');
	}


	/**
	 * Takes care of displaying HTML within the metabox.
	 */
	public function render_metabox($post)
	{
		// Define key variables
		$id = 'shoppme_subscriber_content';
		$status = get_post_meta($post->ID, 'shoppme_memberonly', true); 
		$checked = $status == '1' ? 'checked="checked"' : '';
		$label = __('Reserve this content for subscribers', 'Shopp');

		// Security
		wp_nonce_field(plugin_basename(__FILE__), 'shoppse_poststatus');

		// Print the checkbox
		echo "<label for=\"$id\"> <input type=\"checkbox\" name=\"$id\" id=\"$id\" value=\"1\" $checked > "
			."$label </label>";
	}


	/**
	 * Updates the post status (visible to all or visible only to
	 * subscribers).
	 *
	 * @param int $post_id
	 * @param stdClass $post
	 */
	public function update_post_setting($post_id)
	{
		// Take no action during autosaves
		if (defined('DOING_AUTOSAVE') and DOING_AUTOSAVE) return;

		// Security
		if (!wp_verify_nonce($_POST['shoppme_poststatus'], plugin_basename(__FILE__ )))
      		return;
      	elseif (!current_user_can('edit_post', $post_id)) 
      		return;

      	// Assessment
      	$id = 'shoppme_subscriber_content';
		$status = (array_key_exists($id, $_POST) and $_POST[$id] == '1') ? '1' : '0';

      	// Load the post object and determine the correct post ID
      	$post = get_post($post_id);
      	$post_id = $post->post_parent == 0 ? $post_id : $post->post_parent;

      	// Update the post subscription status
      	update_post_meta($post_id, 'shoppme_memberonly', $status);
	}


	/**
	 * Determines if the current post content should be seen by
	 * the user and replaces it with alternative content if necessary.
	 * For the content to pass unmolested it must:
	 *
	 *		Be explicitly marked as unreserved
	 *		Carry no subscription access flag at all
	 *		Or the user must be logged in with an in-date subscription
	 *
	 * @param string $content
	 * @return string
	 */
	public function access_policy($content)
	{
		// Determine if this is a protected post or not
		global $post;

		$status = get_post_meta($post->ID, 'shoppme_memberonly', true); 
		$protected = $status == '1' ? true : false;

		// If not, return the content as is
		if (!$protected) return $content;

		// Otherwise, is this user allowed access?
		if ($this->magazine->users->is_subscribed())
			return $content;

		// If not then show to the door
		return $this->closed_door_page();
	}


	/**
	 * Loads and returns the designated "no access" page 
	 * or a more conventional message if that has not been
	 * specified.
	 *
	 * @return string
	 */
	protected function closed_door_page()
	{
		// Get the specified page
		$page = get_option('shoppme_doorpage', false);

		// If none is specified then use a default message
		if ($page === false)
			$msg = __('Sorry! You are not allowed to view this content.', 'Shopp');

		// Otherwise ... try loading the specified page
		else
		{
			$page = get_page($page);
			$msg = apply_filters('the_content', $page->post_content);
		}

		return $msg;
	}
}